package main

import (
	"golang.org/x/text/width"
	"strconv"
	"strings"
)

type AppItemTemplateModel struct {
	Url  string
	Text string
}

func (a *AppItemTemplateModel) len(s string) int {
	size := 0
	for _, runeValue := range s {
		p := width.LookupRune(runeValue)
		if p.Kind() == width.EastAsianWide {
			size += 2
			continue
		}
		if p.Kind() == width.EastAsianNarrow {
			size += 1
			continue
		}
		panic("cannot determine!")
	}
	return size
}

func (a *AppItemTemplateModel) FromAppItemReply(reply *AppItemReply) {
	a.Url = reply.Url
	blankCount := 51 - a.len(a.Url)
	a.Text = strings.Repeat(" ", blankCount)
	a.Text += reply.LastModified.Format("02-Jan-2006 15:04")
	size := "-"
	if reply.Size > 0 {
		size = strconv.FormatUint(reply.Size, 10)
	}
	blankCount = 20 - a.len(size)
	a.Text += strings.Repeat(" ", blankCount)
	a.Text += size
}
